package com.weilian.ordering.common;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.weilian.ordering.R;
import com.weilian.ordering.beans.PageBean;
import com.weilian.ordering.util.CacheManager;

public class ImageAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<PageBean> images;
	HashMap<Integer, Bitmap> dataCache = new HashMap<Integer, Bitmap>();

	public ImageAdapter(Context context, ArrayList<PageBean> images) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.images = images;
	}

	public void setData(ArrayList<PageBean> images) {
		this.images = images;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return images.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.image_item, null);
		}
		ImageView image = (ImageView) convertView.findViewById(R.id.imgView);
		PageBean bean = images.get(position);
		Bitmap current = dataCache.get(position);
		if (current != null) {// 如果缓存中已解码该图片，则直接返回缓存中的图片
			image.setImageBitmap(current);
		} else {
			current = getPhotoItem(bean.imagePath, 2);
			image.setImageBitmap(current);
			dataCache.put(position, current);
		}
		return convertView;
	}

	public Bitmap getPhotoItem(String filepath, int size) {
		BitmapFactory.Options options = new BitmapFactory.Options();
//		options.inSampleSize = size;
		Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
//		bitmap = Bitmap.createScaledBitmap(bitmap, 180, 251, true);// 预先缩放，避免实时缩放，可以提高更新率
		return bitmap;
	}

	public SoftReference<Bitmap> loadBitmapFromSDCard(String path) {
		// CacheManager.checkCache();
		BitmapFactory.Options options = new Options();
		// options.inSampleSize = 3;
		// options.inTempStorage = new byte[16 * 1024];
		SoftReference<Bitmap> softReference;
		// Bitmap bitmap = null;
		try {
			// bitmap = BitmapFactory.decodeFile(path, options);
			softReference = new SoftReference<Bitmap>(BitmapFactory.decodeFile(
					path, options));
			CacheManager.add(path, softReference.get());
			return softReference;
		} catch (OutOfMemoryError e) {
			// TODO: handle exception
			CacheManager.clear();
			softReference = new SoftReference<Bitmap>(BitmapFactory.decodeFile(
					path, options));
			CacheManager.add(path, softReference.get());
			return softReference;
		}
	}
}
